/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.schedulerservicecontainer.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPMessageFromEmbeddedViewHandler;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPShell;
import fr.openparts.OpenParts.client.OPSlot;
import fr.openparts.opshowcase.schedulerservicecontainer.client.presenter.SchedulerServiceContainerPresenter;
import fr.openparts.opshowcase.schedulerservicecontainer.client.resources.SchedulerServiceContainerResources;
/**
 * Main view for Scheduler Service Container for Scheduler Module for OpenParts Showcase.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 03 May 2011
 */
public class SchedulerServiceContainerView extends Composite implements OPMessageFromEmbeddedViewHandler {

    interface SchedulerServiceContainerViewUiBinder extends UiBinder<Widget, SchedulerServiceContainerView> {}
    private static SchedulerServiceContainerViewUiBinder uiBinder = GWT.create(SchedulerServiceContainerViewUiBinder.class);

    public static final String SERVICE_EMBEDDED = "schedulerserviceembedded";
    
    protected SchedulerServiceContainerPresenter presenter;

    @UiField
    protected SchedulerServiceContainerResources resources;
    
    @UiField
    protected LayoutPanel container;
    
    /**
     * Constructor.
     */
    public SchedulerServiceContainerView(final SchedulerServiceContainerPresenter presenter) {
        initWidget(uiBinder.createAndBindUi(this));
        resources.style().ensureInjected();
        this.presenter = presenter;
        
        final OPSlot slot = OPShell.getSlot(SERVICE_EMBEDDED, SERVICE_EMBEDDED, OPShell.ACTION_DISPLAY);
        slot.getViewProxy().addMessageHandler(this);
        container.add(slot);
        
        OPShell.displayView(SERVICE_EMBEDDED, SERVICE_EMBEDDED, OPShell.ACTION_DISPLAY, slot);
        
        OPShell.dispatchEvent("genericEvent", new OPParams("value", "service container"));
        
        slot.getViewProxy().dispatchMessageToEmbeddedView("dummyMessage", null);
    }

    /**
     * Called when receiving a message from the embedded view.
     * @see fr.openparts.OpenParts.client.OPMessageFromEmbeddedViewHandler#handleMessageFromEmbeddedView(java.lang.String, fr.openparts.OpenParts.client.OPParams)
     */
    @Override
    public void handleMessageFromEmbeddedView(String message, OPParams params) {
        presenter.log("service container handling message from embedded view");
    }

    /**
     * Called when the embedded view is attached.
     * @see fr.openparts.OpenParts.client.OPMessageFromEmbeddedViewHandler#onEmbeddedViewAttach()
     */
    @Override
    public void onEmbeddedViewAttach() {
        final Element embeddedView = DOM.getElementById("schedulerEmbeddedView");
        final Element modification = DOM.createDiv();
        modification.setInnerText("modification by container view");
        embeddedView.appendChild(modification);        
    }
    
}
